<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('contact_model');	
		$this->load->model('company_model');	
		$this->load->model('titles_model');	
		$this->load->model('rate_model');	
		$this->load->model('previous_event_model','p_event');	
	}
	
	public function index()
	{
		$data['msg'] 	= NULL;
		$this->load->view('contact/main', $data);
	}
	
	function form($id=NULL,$com=NULL)
	{
		//sleep(1);
		
		$data['load_all_companies'] = true;
		
		
		$data['contact']='';
		
		if(!empty($com)){
			$data['com_list'] = $this->company_model->where('deleted',0)->find($com);
			$data['load_all_companies'] = false;
		}else{
			$data['com_list'] = $this->company_model->where('deleted',0)->find_all();	
		}
			
		$data['titles_list'] 	= $this->titles_model->where('deleted',0)->find_all();
		$data['event_list'] 	= $this->p_event->find_all();
		
		if(!empty($id))
			$data['contact'] = $this->contact_model->find($id);
			
		$this->load->view('contact/form', $data);
	}
	
		
	function create($form)
	{
		if($inserted_id = $this->contact_model->insert($form))
			{
				$data['error']= false;
				$data['id']= $inserted_id;
				$data['msg']  = '<span style="color:green">New Company :<b>"'.$form['name_ar'].'"</b> Has Been Added Successfully<br>Thanks You !</span>';
			}
			else{
				$data['error']= true;
				$data['msg']  = 'Data Base Error <br> ERROR: con/ins/100';
			}
			
		return $data;
	}
		
	function edit($id,$form)
	{
		if($this->contact_model->update($id, $form)){
					$data['error']= false;
					$data['msg']= $form['name_ar'].' Successfully Updated..';
				}
				else{
					$data['error']= true;
					$data['msg']  = 'Data Base Error <br> ERROR: con/upd/101';
				}
			
		return $data;
	}	
	function save()
	{
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		//insert
		if(empty($id) && $id=='')
		{
		$this->form_validation->set_rules('name_ar'		, 'Arabic Name'		, 'trim|required|min_length[3]|max_length[255]|is_unique[contacts.name_ar]');
		$this->form_validation->set_rules('name_en'		, 'English Name'	, 'trim|required|min_length[3]|max_length[255]|is_unique[contacts.name_en]');
		$this->form_validation->set_rules('email'		, 'Email'		, 'valid_email|trim|required|max_length[40]|min_length[5]|is_unique[contacts.email]');
		$this->form_validation->set_rules('mobile1'		, 'Mobile 1'	, 'numeric|trim|max_length[20]|min_length[5]|is_unique[contacts.mobile1]');
		$this->form_validation->set_rules('mobile2'		, 'Mobile 2'	, 'numeric|trim|max_length[20]|min_length[5]|is_unique[contacts.mobile2]');
		$this->form_validation->set_rules('fax'			, 'Fax '		, 'numeric|trim|max_length[20]|min_length[5]|is_unique[contacts.fax]');
		}
		
		//update
		if($id && !empty($id))
		{
		//$this->form_validation->set_rules('username', 'User', 'is_unique[t_users.username.id.'.$id.']'); 
		$this->form_validation->set_rules('name_ar'		, 'Arabic Name'		, 'trim|required|min_length[3]|max_length[255]|is_unique2[contacts.name_ar.id.'.$id.']');
		$this->form_validation->set_rules('name_en'		, 'English Name'	, 'trim|required|min_length[3]|max_length[255]|is_unique2[contacts.name_en.id.'.$id.']');
		$this->form_validation->set_rules('email'		, 'Email'		, 'valid_email|trim|required|max_length[40]|min_length[5]|is_unique2[contacts.email.id.'.$id.']');
		$this->form_validation->set_rules('mobile1'		, 'Mobile 1'	, 'numeric|trim|max_length[20]|min_length[5]|is_unique2[contacts.mobile1.id.'.$id.']');
		$this->form_validation->set_rules('mobile2'		, 'Mobile 2'	, 'numeric|trim|max_length[20]|min_length[5]|is_unique2[contacts.mobile2.id.'.$id.']');
		$this->form_validation->set_rules('fax'			, 'Fax '		, 'numeric|trim|max_length[20]|min_length[5]|is_unique2[contacts.fax.id.'.$id.']');
		}
		

		$this->form_validation->set_rules('gender'	, 'Gender'		, 'trim|required');
		$this->form_validation->set_rules('lang'	, 'Contact lang.', 'trim|required');
		$this->form_validation->set_rules('job_des'	, 'Job Title'	, 'trim|required|min_length[2]');
		$this->form_validation->set_rules('country'	, 'Country'		, 'trim|required|max_length[2]|alpha');
		$this->form_validation->set_rules('city'	, 'City'		, 'trim|required|max_length[255]|min_length[3]');
		$this->form_validation->set_rules('address', 'Address '		, 'trim|min_length[5]');
		$this->form_validation->set_rules('company', 'Company'		, 'trim');
		$this->form_validation->set_rules('notes'	, 'Notes'		, 'trim|min_length[5]');
		$this->form_validation->set_rules('extra'	, 'Extra'		, 'trim|min_length[5]');
		$this->form_validation->set_rules('title'	, 'title'		, 'trim|required');
		$this->form_validation->set_rules('website'	, 'website'		, 'trim|prep_url');
		$this->form_validation->set_rules('home_phone', 'Home Phone', 'numeric|trim|max_length[20]|min_length[5]');

				
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();
			
			die(json_encode($data));

			//die(json_encode($errors));
		}
		else
		{
			//ucwords(strtolower($bar));
			$form['name_ar'] 		= ucwords(strtolower($this->input->post('name_ar')));
			$form['name_en'] 		= ucwords(strtolower($this->input->post('name_en')));
			$form['country'] 		= $this->input->post('country');
			$form['lang'] 			= $this->input->post('lang');
			$form['company'] 		= $this->input->post('company');
			$form['city'] 			= ucwords(strtolower($this->input->post('city')));
			$form['job_des'] 		= $this->input->post('job_des');
			$form['address'] 		= $this->input->post('address');
			$form['notes'] 			= $this->input->post('notes');
			$form['extra'] 			= $this->input->post('extra');
			$form['title'] 			= $this->input->post('title');
			$form['website'] 		= $this->input->post('website');
			$form['email'] 			= $this->input->post('email');
			$form['home_phone'] 	= $this->input->post('home_phone');
			$form['mobile1'] 		= $this->input->post('mobile1');
			$form['mobile2'] 		= $this->input->post('mobile2');
			$form['fax'] 			= $this->input->post('fax');
			$form['gender'] 		= $this->input->post('gender');
			$form['previous_event'] = $this->input->post('previous_event');
			
			//NEW (insert)
			if($this->input->post('id')=='')
			{
				$data = $this->create($form);
				$this->log('Contacts', 'Create record with ID: '.$data['id'].' : '.$this->session->userdata['ip_address']);
			}
			
			//Edit (update)
			if($this->input->post('id'))
			{
				$id = $this->input->post('id');
				$data = $this->edit($id, $form);
				$this->log('Contacts', 'Updated record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
			}

			die(json_encode($data));
		}

	}

		
	function delete($id=NULL)
	{
		if (!empty($id))
		{	
			//check if the company has no attached contact in contacts table
			
				if ($this->contact_model->delete($id))
				{
					//success
						$data['msg']	= ' Successfully Deleted..';
						$data['error']	= false;
						$this->log('Contacts', 'Delete record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
					}
				else{
					//failed to delete
					$data['error']	= true;
					$data['msg']	= ' Failed to Delete..';
				}
		die(json_encode($data));
		}
		
	}

	//if no id set then load all contact for (A-Z) list
	//if id is set then load the contact-id info only for the side menu info
	function get_contact_info($id=NULL){
		
		if(!$id)
		{
			if($this->enable_flags){
				//$contacts_id = $this->flag_model->get_contacts_id_by_flag($this->allowed_flags_id);
				$list = $this->contact_model->find_by_flag();
			}
			else
				$list = $this->contact_model->where('deleted',0)->find_all();
			
			//print_r($this->db->last_query());die;
			
				
			$contact_group = array();
			//$data['num_row'] = $list->num_rows();

			foreach($list as $row){
				$contact_group[mb_substr($row->name_ar,0,1,'utf-8')][] = array("id" => $row->id, "name" => $row->name_ar);
			}
			
			$data['list']=$contact_group;
		}
		
		if($id)
		{
			//sleep(1);
			$this->log('Contacts', 'View record with ID: '.$id.' : '.$this->session->userdata['ip_address']);
			$data['list']  = $this->contact_model->find($id);
			//print_r($this->db->last_query());
			
		}

		die(json_encode($data['list']));
		
	}
	
	function get_contact_rate($id=NULL)
	{
		//sleep(1);
		$data['rate']  		= $this->rate_model->avg_contact($id);
		$data['rate_count'] = $this->rate_model->count_rates_per_contact($id);
		die(json_encode($data));
	}
	
	//this function works inside the company view
	// it allow the user to view all contacts inside a company
	function company_contact($company_id)
	{
		$data['list'] = $this->contact_model->where('company',$company_id)->find_all();
		die(json_encode($data['list']));
	}
}
	
